import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk, ImageDraw
import os

class ImageRectangleDrawer:
    def __init__(self, image_path):
        self.root = tk.Tk()
        self.root.title("图片矩形框绘制工具")
        
        # 加载原始图片
        self.original_image = Image.open(image_path)
        self.image_path = image_path
        
        # 创建PhotoImage对象用于显示
        self.photo = ImageTk.PhotoImage(self.original_image)
        
        # 创建画布
        self.canvas = tk.Canvas(
            self.root, 
            width=self.original_image.width,
            height=self.original_image.height
        )
        self.canvas.pack()
        
        # 显示图片
        self.canvas.create_image(0, 0, anchor="nw", image=self.photo)
        
        # 初始化变量
        self.start_x = None
        self.start_y = None
        self.rect_id = None
        self.coordinates_label = ttk.Label(self.root, text="")
        self.coordinates_label.pack()
        
        # 绑定鼠标事件
        self.canvas.bind("<Button-1>", self.start_draw)
        self.canvas.bind("<B1-Motion>", self.draw_rectangle)
        self.canvas.bind("<ButtonRelease-1>", self.end_draw)
        
    def start_draw(self, event):
        self.start_x = event.x
        self.start_y = event.y
        # 如果已经有矩形，则删除
        if self.rect_id:
            self.canvas.delete(self.rect_id)
            
    def draw_rectangle(self, event):
        if self.rect_id:
            self.canvas.delete(self.rect_id)
        self.rect_id = self.canvas.create_rectangle(
            self.start_x, self.start_y,
            event.x, event.y,
            outline='red',
            width=2
        )
        
    def end_draw(self, event):
        end_x, end_y = event.x, event.y
        
        # 确保坐标是从左上到右下
        left = min(self.start_x, end_x)
        top = min(self.start_y, end_y)
        right = max(self.start_x, end_x)
        bottom = max(self.start_y, end_y)
        
        # 计算差值
        width = right - left
        height = bottom - top
        
        # 更新标签显示坐标信息
        coordinates_text = (
            f"左上角坐标: ({left}, {top})\n"
            f"右下角坐标: ({right}, {bottom})\n"
            f"宽度: {width}, 高度: {height}"
        )
        self.coordinates_label.config(text=coordinates_text)
        
        # 在原图上绘制矩形并保存
        self.save_image_with_rectangle(left, top, right, bottom)
        
    def save_image_with_rectangle(self, left, top, right, bottom):
        # 创建原图的副本
        image_with_rect = self.original_image.copy()
        draw = ImageDraw.Draw(image_with_rect)
        
        # 绘制矩形
        draw.rectangle(
            [(left, top), (right, bottom)],
            outline='red',
            width=2
        )
        
        # 生成新文件名
        file_name, file_ext = os.path.splitext(self.image_path)
        new_file_path = f"{file_name}_with_rectangle{file_ext}"
        
        # 保存图片
        image_with_rect.save(new_file_path)
        print(f"已保存标注后的图片到: {new_file_path}")
        
    def run(self):
        self.root.mainloop()

# 使用示例
if __name__ == "__main__":
    # 替换为你的图片路径
    image_path = r"D:\研究生\科研相关\写论文\OE_Figure\Snipaste_2025-02-15_16-35-56.jpg"
    app = ImageRectangleDrawer(image_path)
    app.run()